package com.hm.base.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.base.auto.helper.HmSessionFactory;
import com.hm.base.auto.su.ControllerResult;
import com.hm.base.auto.su.R.Restful;
import com.hm.base.def.RequestMethodEnum;
import com.hm.base.service.ReqLogService;
import com.hm.base.service.SystemConfigDictService;
import com.hm.base.vo.ReqLogVo;
import com.hm.base.vo.SystemConfigDictVo;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.su.bean.ServerResponse;
import com.hm.common.util.StringUtil;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年3月10日 下午1:17:26
 * @version 1.0
 * @describe 日志管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/reqlog")
public class ReqLogApi {

	private static final String SWWAGER_TAG = "操作日志管理";
	
	@Autowired
	private ReqLogService reqLogService;

	@Autowired
	private SystemConfigDictService systemConfigDictService;

	@RequestMapping(value = "/modules/{platform}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "加载业务系统模块列表", httpMethod = "GET", notes = "com.hm.base.api.ReqLogApi::loadBusPlatforModules")
	public ResponseEntity<ServerResponse<List<SystemConfigDictVo>>> loadBusPlatforModules(
			@ApiParam(required = true, name = "platform", value = "业务平台") @PathVariable("platform") String platform) {
		log.debug("用户{},加载业务系统模块列表", HmSessionFactory.currentUserId());
		if(StringUtil.isBlankOrNull(platform)) {
			return ControllerResult.success(new ArrayList<SystemConfigDictVo>(0));
		}
		return ControllerResult.success(systemConfigDictService.loadSystemConfigDictByDictType(platform.trim()));
	}
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页操作日志记录", httpMethod = "GET", notes = "com.hm.base.api.ReqLogApi::query")
	public ResponseEntity<ServerResponse<PageInfo<ReqLogVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "platform", value = "业务系统") @RequestParam(required = false, name = "platform") String platform,
			@ApiParam(required = false, name = "module", value = "业务模块") @RequestParam(required = false, name = "module") String module,
			@ApiParam(required = false, name = "method", value = "请求方式") @RequestParam(required = false, name = "method") RequestMethodEnum method,
			@ApiParam(required = false, name = "startCreateTime", value = "开始日期") @RequestParam(required = false, name = "startCreateTime") Long startCreateTime,
			@ApiParam(required = false, name = "endCreateTime", value = "结束日期") @RequestParam(required = false, name = "endCreateTime") Long endCreateTime)
			throws Exception {
		log.debug("用户{},查询分页操作日志记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(reqLogService.query(new PageParam(pageNumber, pageSize), platform, module,method, startCreateTime, endCreateTime));
	}
}
